package golang

import "gitee.com/allowall-package/goutil/fn"

func BinarySearch(a []int, s int) int {
	n := len(a)

	low := 0
	height := n - 1

	for low <= height {
		m := (low + height) / 2
		if s == a[m] {
			return m
		}

		if s > a[m] {
			low = m + 1
		} else if s < a[m] {
			height = m - 1
		}
	}

	return -1

}

func BinarySearchTest() {
	s := 4
	r := BinarySearch(a, s)
	fn.Dump(fn.FuncInfo(), a, s, r)
}
